package com.ykdl.member.kid.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.res.Resources;
import android.os.Environment;

import com.ykdl.member.kid.R;
import com.ykdl.member.base.QLog;
/**
 * 访问 文件系统的工具类,疫苗通在使用
 */
public class FileHelper {
	private static String HtmlCss = null;
	private static String HtmlCss2 = null;
	private static String HtmlTemplate = null;
	public static final String IREADER_OFFLINE_PAHT = Environment
			.getExternalStorageDirectory().toString() + "/lmyr/ireader";
	public static final String IREADER_YMT_PAHT = Environment
			.getExternalStorageDirectory().toString() + "/lmyr";

	/**
	 * 解压一个压缩文档 到指定位置
	 * 
	 * @param zipFileString
	 *            压缩包的名字
	 * @param outPathString
	 *            指定的路径
	 * @throws Exception
	 */
	public static void UnZipFolder(final int zipFileString,
			final String outPathString, final Resources res) throws Exception {
		new Thread() {
			public void run() {
				try {
					long cur = System.currentTimeMillis();
					java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(
							res.openRawResource(zipFileString));
					java.util.zip.ZipEntry zipEntry;
					String szName = "";

					while ((zipEntry = inZip.getNextEntry()) != null) {
						szName = zipEntry.getName();

						if (zipEntry.isDirectory()) {
							// get the folder name of the widget
							szName = szName.substring(0, szName.length() - 1);
							java.io.File folder = new java.io.File(
									outPathString + java.io.File.separator
											+ szName);
							folder.mkdirs();

						} else {
							java.io.File dir = new java.io.File(outPathString
									+ java.io.File.separator);
							if (!dir.exists()) {
								dir.mkdirs();
							}
							java.io.File file = new java.io.File(outPathString
									+ java.io.File.separator + szName);
							if (file.exists()) {
								continue;
							} else {
								file.createNewFile();
							}
							// get the output stream of the file
							java.io.FileOutputStream out = new java.io.FileOutputStream(
									file);
							int len;
							byte[] buffer = new byte[1024];
							// read (len) bytes into buffer
							while ((len = inZip.read(buffer)) != -1) {
								// write (len) byte from buffer at the position
								// 0
								out.write(buffer, 0, len);
								out.flush();
							}
							out.close();
						}
					}// end of while
					QLog.debug("FileHelper ", "UnZipFolder cost......."
							+ (System.currentTimeMillis() - cur));
					inZip.close();
				} catch (Exception ee) {
					QLog.debug("FileHelper ", "UnZipFolder fail......."
							+ (ee.toString()));
				}

			}

		}.start();

	}// end of func


	public static String getIReaderHtmlContent(String name, String cs,
			boolean checked, Resources res) {
		String file = IREADER_YMT_PAHT + "/ymt/" + name;
		String content = null;
		try {
			if (HtmlCss2 == null) {
				// 读css的文件
				InputStream css = res.openRawResource(R.raw.ymt_ios);
				int leng = css.available();
				byte[] csss = new byte[leng];
				css.read(csss);
				css.close();
				HtmlCss2 = new String(csss);
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(file)));
			StringBuffer sb = new StringBuffer();
			while ((content = br.readLine()) != null) {
				if (content.contains("第一次")) {
					String oldstr = "第一次";
					String newstr = cs;
					content = content.replace(oldstr, newstr);
				}
				if (content.contains("onclick")) {
					if (checked == true) {
						content = content.replace("onclick", "checked onclick");
					}
				}
				if (content.contains("ymt_ios")) {
					content = HtmlCss2;
				}
				sb.append(content);
			}
			content = sb.toString();

			br.close();

		} catch (Exception e) {
		}
		return content;
	}
}
